#include "vtkTipsCoordinate.h"

#include <vtkNamedColors.h>
#include <vtkPlaneSource.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkVectorText.h>

vtkStandardNewMacro(vtkTipsCoordinate);

vtkTipsCoordinate::~vtkTipsCoordinate()
{
}


vtkTipsCoordinate::vtkTipsCoordinate()
{
    vtkNew<vtkNamedColors> colors;
    // 创建三个箭头
    vtkSmartPointer<vtkArrowSource> arrowSourceX = vtkSmartPointer<vtkArrowSource>::New();
    arrowSourceX->SetArrowOriginToDefault();
    arrowSourceX->SetTipResolution(12);
    arrowSourceX->SetTipLength(0.08);
    arrowSourceX->SetTipRadius(0.03);
    arrowSourceX->SetShaftRadius(0.005);
    arrowSourceX->SetShaftResolution(12);
    arrowSourceX->Update();

    vtkSmartPointer<vtkPolyDataMapper> arrowMapperX = vtkSmartPointer<vtkPolyDataMapper>::New();
    arrowMapperX->SetInputConnection(arrowSourceX->GetOutputPort());

    arrowActorX = vtkSmartPointer<vtkActor>::New();
    arrowActorX->SetMapper(arrowMapperX);
    arrowActorX->GetProperty()->SetColor(colors->GetColor4d("Black").GetData());

    // 文字信息
    vtkSmartPointer<vtkVectorText> arrowActorX_textSource = vtkSmartPointer<vtkVectorText>::New();
    arrowActorX_textSource->SetText("X");
    vtkSmartPointer<vtkPolyDataMapper> arrowActorX_textMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    arrowActorX_textMapper->SetInputConnection(arrowActorX_textSource->GetOutputPort());
    arrowActorX_text = vtkSmartPointer<vtkFollower>::New();
    arrowActorX_text->SetMapper(arrowActorX_textMapper);
    arrowActorX_text->SetScale(0.1);
    arrowActorX_text->GetProperty()->SetColor(colors->GetColor4d("Black").GetData());
    arrowActorX_text->SetPosition(1.01, 0.06, 0);

    vtkSmartPointer<vtkArrowSource> arrowSourceY = vtkSmartPointer<vtkArrowSource>::New();
    arrowSourceY->SetArrowOriginToDefault();
    arrowSourceY->SetTipResolution(12);
    arrowSourceY->SetTipLength(0.08);
    arrowSourceY->SetTipRadius(0.03);
    arrowSourceY->SetShaftRadius(0.005);
    arrowSourceY->SetShaftResolution(12);
    arrowSourceY->Update();

    vtkSmartPointer<vtkPolyDataMapper> arrowMapperY = vtkSmartPointer<vtkPolyDataMapper>::New();
    arrowMapperY->SetInputConnection(arrowSourceY->GetOutputPort());

    arrowActorY = vtkSmartPointer<vtkActor>::New();
    arrowActorY->SetMapper(arrowMapperY);
    arrowActorY->GetProperty()->SetColor(colors->GetColor4d("Black").GetData()); 

    // 文字信息
    vtkSmartPointer<vtkVectorText> arrowActorY_textSource = vtkSmartPointer<vtkVectorText>::New();
    arrowActorY_textSource->SetText("Y");
    vtkSmartPointer<vtkPolyDataMapper> arrowActorY_textMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    arrowActorY_textMapper->SetInputConnection(arrowActorY_textSource->GetOutputPort());
    arrowActorY_text = vtkSmartPointer<vtkFollower>::New();
    arrowActorY_text->SetMapper(arrowActorY_textMapper);
    arrowActorY_text->SetScale(0.1);
    arrowActorY_text->GetProperty()->SetColor(colors->GetColor4d("Black").GetData());
    arrowActorY_text->SetPosition(0.06, 1.01, 0);

    vtkSmartPointer<vtkArrowSource> arrowSourceZ = vtkSmartPointer<vtkArrowSource>::New();
    arrowSourceZ->SetArrowOriginToDefault();
    arrowSourceZ->SetTipResolution(12);
    arrowSourceZ->SetTipLength(0.08);
    arrowSourceZ->SetTipRadius(0.03);
    arrowSourceZ->SetShaftRadius(0.005);
    arrowSourceZ->SetShaftResolution(12);
    arrowSourceZ->Update();

    vtkSmartPointer<vtkPolyDataMapper> arrowMapperZ = vtkSmartPointer<vtkPolyDataMapper>::New();
    arrowMapperZ->SetInputConnection(arrowSourceZ->GetOutputPort());

    arrowActorZ = vtkSmartPointer<vtkActor>::New();
    arrowActorZ->SetMapper(arrowMapperZ);
    arrowActorZ->GetProperty()->SetColor(colors->GetColor4d("Black").GetData());

    // 文字信息
    vtkSmartPointer<vtkVectorText> arrowActorZ_textSource = vtkSmartPointer<vtkVectorText>::New();
    arrowActorZ_textSource->SetText("Z");
    vtkSmartPointer<vtkPolyDataMapper> arrowActorZ_textMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    arrowActorZ_textMapper->SetInputConnection(arrowActorZ_textSource->GetOutputPort());
    arrowActorZ_text = vtkSmartPointer<vtkFollower>::New();
    arrowActorZ_text->SetMapper(arrowActorZ_textMapper);
    arrowActorZ_text->SetScale(0.1);
    arrowActorZ_text->GetProperty()->SetColor(colors->GetColor4d("Black").GetData());
    arrowActorZ_text->SetPosition(0.06, 0, 1.01);

    arrowActorX->SetOrientation(90, 0, 0);
    arrowActorX->SetPosition(0, 0, 0);
    arrowActorY->SetOrientation(0, 0, 90);
    arrowActorY->SetPosition(0, 0, 0);
    arrowActorZ->SetOrientation(0, -90, 0);
    arrowActorZ->SetPosition(0, 0, 0);

    m_coordAsm = vtkAssembly::New();
    m_coordAsm->SetPickable(false);
}

void vtkTipsCoordinate::SetRenderer(vtkSmartPointer<vtkRenderer> renderer)
{
    m_renderer = renderer;
}

void vtkTipsCoordinate::AddToRenderer()
{
    if (!m_renderer)
    {
        return;
    }

    //vtkCamera* camera = m_renderer->GetActiveCamera();
    //arrowActorX_text->SetCamera(camera);
    //arrowActorY_text->SetCamera(camera);
    //arrowActorZ_text->SetCamera(camera);

    m_coordAsm->AddPart(arrowActorX);
    m_coordAsm->AddPart(arrowActorY);
    m_coordAsm->AddPart(arrowActorZ);
    m_coordAsm->AddPart(arrowActorX_text);
    m_coordAsm->AddPart(arrowActorY_text);
    m_coordAsm->AddPart(arrowActorZ_text);

    m_renderer->AddActor(m_coordAsm);
}